/**
 * @brief 键盘扫描函数
 * @param 无
 * @return 无
 * @note 使用前必须确定使用哪种键盘，单键还是矩阵键盘，并在下面选择相应的宏定义
 */
#include "keyboard.h"

KEYStruct key;                                   

KEYStruct* GetKeyStruct(void)                 
{
    return &key;             	
}


#ifdef USE_MATRIX_KEY_S4


//清除列(列线清零，行线拉高)
static void ClsC(void)
{
	P44 = 0;
	P42 = 0;
	P32 = 1;
	P33 = 1;
}

//清除行(行线清零，列线拉高)
static void ClsR(void)
{
	P32 = 0;
	P33 = 0;
	P44 = 1;
	P42 = 1;		
}

//
/**
 * 扫描键盘，获取按键值
 * value:1-> s4
 * value:3-> s5
 * value:2-> s8
 * value:4-> s9
**/
void ScanKeyboard(void)
{
	static unsigned char col = 0;
	static unsigned int keycounter = 0;
	static unsigned char mid_value = 0;
	
	switch(key.status)
    {
		case 0:
			ClsR();
			if((P44 == 0)||(P42 == 0)){
				key.status = 1;
			}
			break;
		case 1:
			ClsR();
			if(P44 == 0){
				col = 1;
				key.status = 2;
			}else if(P42 == 0){
				col = 2;
				key.status = 2;
			}
			break;
		case 2:
			ClsC();
			if(P32 == 0){
				mid_value = col + 2;
				key.status = 3;
			}else if(P33 == 0){
				mid_value = col;
				key.status = 3;
			}else{
				key.status = 0;
			}
			break;
		case 3:
			ClsC();	
			if((P32 == 1)&&(P33 == 1)){
				if(keycounter < 200){
					key.value = mid_value;
				}else{
					key.value = (mid_value == 4)?('L'):(mid_value);
				}
				keycounter = 0;	
				key.status = 0;
			}else{
				if(keycounter < 200){
					++keycounter;
				}
			}	
			break;
	}
}

void InitKeyStruct(void)
{
    key.pFun = ScanKeyboard;
    key.value = 0xFF;
    key.status = 0;
}

#endif




#ifdef USE_MATRIX_KEY_S12




//清除列(列线清零，行线拉高)
static void ClsC(void)
{
	P35 = 0;
	P34 = 0;
	P32 = 1;
	P33 = 1;
}

//清除行(行线清零，列线拉高)
static void ClsR(void)
{
	P32 = 0;
	P33 = 0;
	P35 = 1;
	P34 = 1;		
}

//
/**
 * 扫描键盘，获取按键值
 * value:1-> s4
 * value:3-> s5
 * value:2-> s8
 * value:4-> s9
**/
void ScanKeyboard(void)
{
	static unsigned char col = 0;
	static unsigned char mid_value = 0;
	
	switch(key.status)
    {
		case 0:
			ClsR();
			if((P35 == 0)||(P34 == 0)){
				key.status = 1;
			}
			break;
		case 1:
			ClsR();
			if(P35 == 0){
				col = 1;
				key.status = 2;
			}else if(P34 == 0){
				col = 2;
				key.status = 2;
			}
			break;
		case 2:
			ClsC();
			if(P32 == 0){
				mid_value = col + 2;
				key.status = 3;
			}else if(P33 == 0){
				mid_value = col;
				key.status = 3;
			}else{
				key.status = 0;
			}
			break;
		case 3:
			ClsC();
			if((P32 == 1) && (P33 == 1))
			{
				key.value = mid_value;
				key.status = 0;
			}
			else if(P32 == 0)
			{
				key.value = 5;
			}
			break;
	}
}

void InitKeyStruct(void)
{
    key.pFun = ScanKeyboard;
    key.value = 0xFF;
    key.status = 0;
}

#endif



#ifdef USE_SINGLE_KEY

sbit S4 = P3^3;  // 独立按键S4
sbit S5 = P3^2;  // 独立按键S5
sbit S6 = P3^1;  // 独立按键S6
sbit S7 = P3^0;  // 独立按键S7

// 清除所有按键检测线（根据硬件设计调整）
static void ClsAll(void)
{
	S4 = 1;
	S5 = 1;
	S6 = 1;
	S7 = 1;
}

// 扫描状态机
void ScanKeys(void)
{
    static unsigned char key_temp = 0;
    
    switch(key.status) {
        case 0: // 状态0：检测是否有按键按下
            ClsAll();
            if (S4 == 0 || S5 == 0 || S6 == 0 || S7 == 0) { // 任意按键按下
    
                key.status = 1;        // 进入消抖确认
            }
            break;
            
        case 1: // 状态1：消抖确认（10ms后再次检测）
            ClsAll();
            if(S4 == 0) { // 确认按键稳定
                key_temp = 4;      
                key.status = 2;             
            }else if(S5 == 0){
				key_temp = 5;
                key.status = 2;
            }else if(S6 == 0){
				key_temp = 6;
				key.status = 2;
			}else if(S7 == 0){
				key_temp = 7;
				key.status = 2;
			}else{
				key.status = 0;
			}
            break;
            
        case 2: // 状态2：等待按键释放
            ClsAll();
			if((S4 == 1) && (S5 == 1) && (S6 == 1) && (S7 == 1))
			{
				key.value = key_temp;
				key.status = 0;
			}
            break;
    }
}


// 初始化函数
void InitKeyStruct(void)
{
    key.pFun = ScanKeys;
    key.value = 0xFF;
    key.status = 0;
}

#endif